package com.xinchuang.service.targetSystem.Impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xinchuang.common.KeyWordSystem;
import com.xinchuang.entity.target.TargetBaseData;
import com.xinchuang.entity.targetSystem.scene.SceneSubList;
import com.xinchuang.entity.targetSystem.scene.SceneTargetList;
import com.xinchuang.entity.targetSystem.system.*;
import com.xinchuang.entity.targetSystem.target.*;
import com.xinchuang.entity.targetSystem.weapon.WeaponRelation;
import com.xinchuang.mapper.targetSystem.SystemPositionMapper;
import com.xinchuang.mapper.targetSystem.SystemsRelationMapper;
import com.xinchuang.mapper.targetSystem.SystemsRelationshipMapper;
import com.xinchuang.mapper.targetSystem.target.SystemsElseMapper;
import com.xinchuang.mapper.targetSystem.target.SystemsRangeMapper;
import com.xinchuang.mapper.targetSystem.target.SystemsRangeTypeMapper;
import com.xinchuang.service.RedisUseService;
import com.xinchuang.service.target.TargetBaseDataService;
import com.xinchuang.service.targetSystem.DeleteService;
import com.xinchuang.service.targetSystem.SystemEmulateDataService;
import com.xinchuang.service.targetSystem.SystemsDynamicService;
import com.xinchuang.service.targetSystem.scene.SceneSubListService;
import com.xinchuang.service.targetSystem.scene.SceneTargetListService;
import com.xinchuang.service.targetSystem.target.*;
import com.xinchuang.util.RedisUtil;
import com.xinchuang.utils.RedisName;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * TODO
 *
 * @author zkwczx
 * @version 1.0
 * @date 2024/4/16 16:04
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class DeleteServiceImpl implements DeleteService {

    private final RedisUtil redisUtil;

    private final RedisUseService redisUseService;

    private final SystemIndexService systemIndexService;

    private final TargetBaseDataService targetBaseDataService;

    private final TargetMsgTableService targetMsgTableService;

    private final TargetSubMsgTableService targetSubMsgTableService;

    private final TargetSubRelationTableService targetSubRelationTableService;

    private final SystemPositionMapper systemPositionMapper;

    private final TargetPostureMsgTableService targetPostureMsgTableService;

    private final TargetPostureRelationTableService targetPostureRelationTableService;

    private final KeyWordSystem keyWordSystem;

    private final SystemsElseMapper systemsElseMapper;

    private final SystemsRangeMapper systemsRangeMapper;

    private final SystemsRelationMapper systemsRelationMapper;

    private final SystemsRelationshipMapper systemsRelationshipMapper;

    private final SystemsRangeTypeMapper systemsRangeTypeMapper;

    private final SystemEmulateDataService systemEmulateDataService;

    private final SceneTargetListService sceneTargetListService;

    private final SceneSubListService sceneSubListService;

    private final TargetRelationshipService targetRelationshipService;

    private final SystemsDynamicService systemsDynamicService;

    @Override
    public void deleteTarget(TargetBaseData target) {
        //删除基础数据
        targetMsgTableService.lambdaUpdate().eq(TargetMsgTable::getRedId, target.getTargetId()).remove();
        redisUtil.hdel(this.keyWordSystem.getVERSION() + "-COMMON-SORT", target.getTargetId());
        //删除目标的指标
        systemIndexService.lambdaUpdate().eq(SystemIndex::getRedId, target.getTargetId()).remove();
        //缓存数据删除
        redisUseService.deleteTargetRedisData(target.getTargetId());
        //删除子目标关联
        List<TargetSubRelationTable> relationTables = targetSubRelationTableService
                .lambdaQuery()
                .eq(TargetSubRelationTable::getTargetId, target.getTargetId())
                .list();
        if (CollectionUtil.isNotEmpty(relationTables)) {
            List<String> subIds = new ArrayList<>();
            for (TargetSubRelationTable relationTable : relationTables) {
                subIds.add(relationTable.getSubId());
                redisUseService.deleteSubRedisData(relationTable.getSubId());
                redisUseService.deleteTargetRangeValue(relationTable.getSubId(), false);
            }
            targetSubRelationTableService.lambdaUpdate().eq(TargetSubRelationTable::getTargetId, target.getTargetId()).remove();
            targetSubMsgTableService.lambdaUpdate().in(TargetSubMsgTable::getSubId, subIds).remove();
        }
        //删除态势关联
        List<TargetPostureRelationTable> postureRelationTables = targetPostureRelationTableService
                .lambdaQuery()
                .eq(TargetPostureRelationTable::getTargetId, target.getTargetId())
                .list();
        if (CollectionUtil.isNotEmpty(postureRelationTables)) {
            List<String> postureIds = new ArrayList<>();
            for (TargetPostureRelationTable postureRelationTable : postureRelationTables) {
                postureIds.add(postureRelationTable.getPostureId());
                redisUseService.deleteSubRedisData(postureRelationTable.getPostureId());
            }
            targetPostureRelationTableService.lambdaUpdate().eq(TargetPostureRelationTable::getTargetId, target.getTargetId()).remove();
            targetPostureMsgTableService.lambdaUpdate().in(TargetPostureMsgTable::getPostureId, postureIds).remove();
        }
        //清理场景数据
        systemPositionMapper.deleteById(target.getTargetId());
        //删除目标实体
        targetBaseDataService.removeById(target.getTargetId());
        //删除目标体系关联
        systemsElseMapper.delete(new LambdaQueryWrapper<SystemsElse>().eq(SystemsElse::getRelId, target.getTargetId()));
        systemsRangeMapper.delete(new LambdaQueryWrapper<SystemsRange>().eq(SystemsRange::getTargetId, target.getTargetId()));
        systemsRangeTypeMapper.delete(new LambdaQueryWrapper<SystemsRangeType>().eq(SystemsRangeType::getTargetId, target.getTargetId()));
        systemsRelationMapper.delete(new LambdaQueryWrapper<SystemsRelation>().eq(SystemsRelation::getTargetId, target.getTargetId()));
        systemsRelationshipMapper.deleteShipTargetId(target.getTargetId());
        systemEmulateDataService.lambdaUpdate().eq(SystemEmulateData::getTargetId, target.getTargetId()).remove();
        //删除目标作用范围
        redisUseService.deleteTargetRangeValue(target.getTargetId(), false);
        redisUtil.setRemove(RedisName.IS_TARGET, target.getTargetId());
        //缓存数据
        redisUseService.deleteIndexLevelValue(target.getTargetId(), "1", null);
        //体系分析数据
        sceneTargetListService.lambdaUpdate().eq(SceneTargetList::getTargetId, target.getTargetId()).remove();
        //体系目标分析
        systemsDynamicService.lambdaUpdate().eq(SystemsDynamic::getTargetId, target.getTargetId()).remove();
        /**
         //删除目标后，修改价值分析影响
         knowService.deleteTargetValue(id);
         //删除资料与标签
         targetElseMapper.deleteTargetElseByTargetId(id);
         //想定关系删除
         ideaMapper.deleteIdeaTarget(id);
         **/
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteSub(String targetId, String subId) {
        //删除数据库数据
        targetSubRelationTableService.lambdaUpdate().eq(TargetSubRelationTable::getSubId, subId).remove();
        targetRelationshipService.lambdaUpdate().eq(TargetRelationship::getSubOne, subId).remove();
        targetRelationshipService.lambdaUpdate().eq(TargetRelationship::getSubTwo, subId).remove();
        targetSubMsgTableService.lambdaUpdate().eq(TargetSubMsgTable::getSubId, subId).remove();
        //体系分析数据
        sceneSubListService.lambdaUpdate().eq(SceneSubList::getSubId, subId).remove();
        //删除缓存
        redisUseService.deleteSubRedisData(subId);
        redisUseService.updateTargetSubRelation(false, targetId, subId);
        redisUtil.setRemove(RedisName.IS_SUB, subId);
        //缓存数据
        redisUseService.deleteIndexLevelValue(subId, "1", null);
        /**
         //删除资料与标签
         targetElseMapper.deleteTargetElseBySubId(subId);
         //判断是否为武器库数据
         WeaponRelation weaponRelation = weaponHouseMapper.selectWeaponTargetRelationOnly(subId);
         if (ObjectUtil.isNotNull(weaponRelation)) {
         weaponHouseMapper.delWeaponTargetRelation(subId);
         }
         //删除子目标后，修改价值分析影响
         knowService.deleteSubTargetValue(subId);
         **/
    }

    @Override
    public void deletePosture(String targetId, String postureId) {
        targetPostureRelationTableService.lambdaUpdate().eq(TargetPostureRelationTable::getPostureId, postureId).remove();
        targetPostureMsgTableService.lambdaUpdate().eq(TargetPostureMsgTable::getPostureId, postureId).remove();
        redisUseService.deleteSubRedisData(postureId);
        redisUseService.deletePosture(targetId, postureId, false);
    }

}
